import { debounce } from "lodash-es";
import { ServerEventListener } from "@/pages/room/ws/ws.ts";
import type { ReactiveData } from "@/pages/room/model.ts";
import type { PlayerInst } from "@/pages/room/components/player/player.ts";

export function createVideoSpeedListener(
    data: ReactiveData,
    player: PlayerInst,
    triggerAfter: (operator: number, speed: number) => void
): ServerEventListener<"video.speed"> {
    return debounce((details) => {
        if (player.getSpeed() !== details.speed) {
            player.speed(details.speed, true);
            triggerAfter(details.operator, details.speed);
        }
    }, 200);
}